package com.cjkj.equipment.service.impl;

import com.cjkj.equipment.domain.EquipmentMonitorRecords;
import com.cjkj.equipment.domain.EquipmentTJ03MonitorRecords;
import com.cjkj.equipment.domain.EquipmentTj03MonitorRecordsHr;
import com.cjkj.equipment.domain.dto.EquipmentHistoryDataRequest;
import com.cjkj.equipment.domain.vo.TJ03RealTimeData;
import com.cjkj.equipment.mapper.EquipmentMonitorRecordsMapper;
import com.cjkj.equipment.mapper.EquipmentTJ03MonitorRecordsMapper;
import com.cjkj.equipment.mapper.EquipmentTj03MonitorRecordsHrMapper;
import com.cjkj.equipment.service.IEquipmentMonitorRecordsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 设备监测数据记录Service业务层处理
 * 
 * @author cjkj
 * @date 2024-08-15
 */
@Service
public class EquipmentMonitorRecordsServiceImpl implements IEquipmentMonitorRecordsService
{
    @Resource
    private EquipmentMonitorRecordsMapper equipmentMonitorRecordsMapper;

    @Resource
    private EquipmentTJ03MonitorRecordsMapper tj03MonitorRecordsMapper;

    @Resource
    private EquipmentTj03MonitorRecordsHrMapper hrMapper;

    /**
     * 新增设备监测数据记录
     *
     * @param equipmentMonitorRecords 设备监测数据记录
     * @return 结果
     */
    @Override
    public int insertEquipmentMonitorRecords(EquipmentMonitorRecords equipmentMonitorRecords) {
        return equipmentMonitorRecordsMapper.insertEquipmentMonitorRecords(equipmentMonitorRecords);
    }

    @Override
    public List<EquipmentMonitorRecords> getList(EquipmentHistoryDataRequest request) {
        return equipmentMonitorRecordsMapper.selectList(request);
    }

    @Override
    public int insertTJ03MonitorRecords(EquipmentTJ03MonitorRecords tj03MonitorRecords) {
        return tj03MonitorRecordsMapper.insertTJ03MonitorRecords(tj03MonitorRecords);
    }

    @Override
    public List<EquipmentTJ03MonitorRecords> getTJ03Records(EquipmentHistoryDataRequest request) {
        return tj03MonitorRecordsMapper.selectTJ03Records(request);
    }

    @Override
    public TJ03RealTimeData getTJ03RealtimeData(String equipmentNo, Date startTime) {
        TJ03RealTimeData data = new TJ03RealTimeData();
        // 查询最新一条监测数据
        EquipmentTJ03MonitorRecords equipmentTJ03MonitorRecords = tj03MonitorRecordsMapper.selectLastRecord(equipmentNo, startTime);
        if(equipmentTJ03MonitorRecords != null) {
            Date recordTime = equipmentTJ03MonitorRecords.getCreateTime();
            data.setUpdateTime(recordTime);

            data.setSbp(equipmentTJ03MonitorRecords.getSbp() == null ? 0 : equipmentTJ03MonitorRecords.getSbp());
            data.setDbp(equipmentTJ03MonitorRecords.getDbp() == null ? 0 : equipmentTJ03MonitorRecords.getDbp());
            data.setPValue(equipmentTJ03MonitorRecords.getPValue() == null ? 0L : equipmentTJ03MonitorRecords.getPValue());
            data.setBptStatus(equipmentTJ03MonitorRecords.getBptStatus() == null ? 0 : equipmentTJ03MonitorRecords.getBptStatus());
            data.setBptResult(equipmentTJ03MonitorRecords.getBptResult() == null ? 0 : equipmentTJ03MonitorRecords.getBptResult());
            data.setBpTime(recordTime);

            data.setBatpt(equipmentTJ03MonitorRecords.getBatpt() == null ? 0 : equipmentTJ03MonitorRecords.getBatpt());
            data.setWState(equipmentTJ03MonitorRecords.getWState() == null ? 0 : equipmentTJ03MonitorRecords.getWState());
            data.setRestHeart(equipmentTJ03MonitorRecords.getRestHeart() == null ? 1 : equipmentTJ03MonitorRecords.getRestHeart());

            data.setBpttStatus(equipmentTJ03MonitorRecords.getBpttStatus() == null ? 0 : equipmentTJ03MonitorRecords.getBpttStatus());
            data.setBpttResult(equipmentTJ03MonitorRecords.getBpttResult() == null ? 0 : equipmentTJ03MonitorRecords.getBpttResult());
            data.setInflDur(equipmentTJ03MonitorRecords.getInflDur() == null ? 0L : equipmentTJ03MonitorRecords.getInflDur());
            data.setBpttTime(recordTime);

            data.setEcgPatchStatus(equipmentTJ03MonitorRecords.getEcgPatchStatus() == null ? 1 : equipmentTJ03MonitorRecords.getEcgPatchStatus());
            data.setEcgPatchPlginStatus(equipmentTJ03MonitorRecords.getEcgPatchPlginStatus() == null ? 0 : equipmentTJ03MonitorRecords.getEcgPatchPlginStatus());

            // 查询穿戴状态
            if(equipmentTJ03MonitorRecords.getWState() == null) {
                EquipmentTJ03MonitorRecords wStateRecord = tj03MonitorRecordsMapper.selectLastWStateDataIsNotNullRecord(equipmentNo, startTime);
                if(wStateRecord != null) {
                    data.setWState(wStateRecord.getWState());
                }
            }
            // 查询故障状态
            if(equipmentTJ03MonitorRecords.getRestHeart() == null) {
                EquipmentTJ03MonitorRecords restHeartRecord = tj03MonitorRecordsMapper.selectLastRestHeartDataIsNotNullRecord(equipmentNo, startTime);
                if(restHeartRecord != null) {
                    data.setRestHeart(restHeartRecord.getRestHeart());
                }
            }

            // 查询血压不为空的数据
            if(equipmentTJ03MonitorRecords.getSbp() == null || equipmentTJ03MonitorRecords.getDbp() == null) {
                EquipmentTJ03MonitorRecords bpDataIsNotNull = tj03MonitorRecordsMapper.selectLastBPDataIsNotNullRecord(equipmentNo, startTime);
                if(bpDataIsNotNull != null) {
                    data.setSbp(bpDataIsNotNull.getSbp());
                    data.setDbp(bpDataIsNotNull.getDbp());
                    data.setPValue(bpDataIsNotNull.getPValue());
                    data.setBptStatus(bpDataIsNotNull.getBptStatus());
                    data.setBptResult(bpDataIsNotNull.getBptResult());
                    data.setBpTime(bpDataIsNotNull.getCreateTime());
                }
            }

            // 查询气密性相关参数不为空的数据
            if(equipmentTJ03MonitorRecords.getBpttStatus() == null || equipmentTJ03MonitorRecords.getBpttResult() == null) {
                EquipmentTJ03MonitorRecords bpttDataIsNotNull = tj03MonitorRecordsMapper.selectLastBPTTDataIsNotNullRecord(equipmentNo, startTime);
                if (bpttDataIsNotNull != null) {
                    data.setBpttStatus(bpttDataIsNotNull.getBpttStatus());
                    data.setBpttResult(bpttDataIsNotNull.getBpttResult());
                    data.setInflDur(bpttDataIsNotNull.getInflDur());
                    data.setBpttTime(bpttDataIsNotNull.getCreateTime());
                }
            }

            // 查询心电贴状态
            if(equipmentTJ03MonitorRecords.getEcgPatchStatus() == null) {
                EquipmentTJ03MonitorRecords ecgPatchRecord = tj03MonitorRecordsMapper.selectLastEcgPatchStatusDataIsNotNullRecord(equipmentNo, startTime);
                if(ecgPatchRecord != null) {
                    data.setEcgPatchStatus(ecgPatchRecord.getEcgPatchStatus());
                }
            }
            if(equipmentTJ03MonitorRecords.getEcgPatchPlginStatus() == null) {
                EquipmentTJ03MonitorRecords ecgPatchPlginStatusRecords = tj03MonitorRecordsMapper.selectLastEcgPatchPlginStatusDataIsNotNullRecord(equipmentNo, startTime);
                if(ecgPatchPlginStatusRecords != null) {
                    data.setEcgPatchPlginStatus(ecgPatchPlginStatusRecords.getEcgPatchPlginStatus());
                }
            }

            // 查询最新的心率
            EquipmentTj03MonitorRecordsHr recordsHr = hrMapper.selectLastHrDataByEquipment(equipmentNo, startTime);
            data.setHr(recordsHr != null ? (recordsHr.getHr() == null ? 0 : recordsHr.getHr()) : 0);
            data.setHrTime(recordsHr != null && recordsHr.getCreateTime() != null ? recordsHr.getCreateTime() : null);
        }

        return data;
    }
}
